import store from './store'
import axios from 'axios'
import ElementUI from 'element-ui';
import router from './router'

axios.defaults.baseURL ="http://localhost:8081"

axios.interceptors.request.use(config => {
  let token = localStorage.getItem('Authorization')
  if(token != '') {
    config.headers["Authorization"] = token;
  }
  
  return config
})

axios.interceptors.response.use(response => {
    let res = response.data;
    if(res.code == 200) {
       return response 
    }else {
    //  ElementUI.$message.error(response.data.msg);
      return Promise.reject(response.data.msg)
    }
},error =>{

  if(error.response.data.msg) {
    error.msg = error.response.data.msg
  }
  if(error.response.status == 401) {
    store.commit("REMOVE_INFO")
    router.push("/login")
  }
  ElementUI.Message.error(error.msg);
  return Promise.reject(error)
})